package com.robinmouren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.robinmouren.constant.MessageConstant;
import com.robinmouren.dto.QuotationPageDto;
import com.robinmouren.entity.ClientContract;
import com.robinmouren.entity.OrderDetail;
import com.robinmouren.entity.OrderInfo;
import com.robinmouren.exception.NullInfoException;
import com.robinmouren.mapper.ClientContractMapper;
import com.robinmouren.service.IClientContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.robinmouren.utils.ListUtils;
import com.robinmouren.utils.StringUtils;
import com.robinmouren.vo.ClientContractPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户合同表 服务实现类
 * </p>
 *
 * @author Robinmouren
 * @since 2024-05-24
 */
@Service
public class ClientContractServiceImpl extends ServiceImpl<ClientContractMapper, ClientContract> implements IClientContractService {

    @Autowired
    private OrderInfoServiceImpl orderInfoService;
    @Autowired
    private ClientContractMapper clientContractMapper;
    @Autowired
    private OrderDetailServiceImpl orderDetailService;


    /**
     * 客户合同分页查询
     * mapper层使用多表连接分页查询，解决前端选择器选择不同分页条件的情况
     * @param dto
     * @return
     */
    @Override
    public Page<ClientContractPageVo> clientPage(QuotationPageDto dto) {

        // 获取dto中的信息
        String input = dto.getInput();
        String value1 = dto.getValue1();

        // 分页查询
        Page<ClientContract> contractPage = new Page<>(dto.getPage(), dto.getPageSize());
        Page<ClientContractPageVo> page = clientContractMapper.clientContractPage(contractPage, dto);

        // 获取货物名称
        List<ClientContractPageVo> records = page.getRecords();
        List<ClientContractPageVo> collect = records.stream().peek(item -> {
            OrderInfo orderinfo = orderInfoService.getOne(new LambdaQueryWrapper<OrderInfo>()
                    .eq(OrderInfo::getOrderNumber, item.getOrderNumber()));
            if (orderinfo == null) {
                throw new NullInfoException(MessageConstant.ORDER_IS_NULL);
            }
            Long orderId = orderinfo.getId();
            List<OrderDetail> list = orderDetailService.list(new LambdaQueryWrapper<OrderDetail>()
                    .eq(OrderDetail::getOrderId, orderId));

            item.setGoodsName(StringUtils.fetchNameInList(list));
        }).collect(Collectors.toList());

        if(Objects.equals(value1, "货名") && !Objects.equals(input, "")){
            // 处理货名信息
            List<ClientContractPageVo> list = new ArrayList<>();
            collect.forEach(item -> {
                if(item.getGoodsName().contains(input)){
                    list.add(item);
                }
            });
            page.setRecords(list);
            return page;
        }

        page.setRecords(collect);
        return page;
    }
}
